//
//  ContactPersonVC.m
//  timelyRain
//
//  Created by Jiafei Wei on 2016/12/22.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "ContactPersonVC.h"
#import "ContactPersonCell.h"
#import "PersonHeadPicture.h"
#import "PersonSex.h"
#import "PersonSign.h"
#import "ContactPersonNick.h"
#import "PersonCityChoose.h"
#import "BindTool.h"
#import "HttpTool.h"
#import "ContactPersonInfo.h"
#import "DbManage.h"
#import "MJExtension.h"
#import "RunSucessInfo.h"
@interface ContactPersonVC ()<UITableViewDelegate,UITableViewDataSource>{
    NSString *url;
    NSString *nick;
    NSString *cityName;
    NSString *sign;
    NSString *sex;
    ContactPersonInfo *personInfo;
    NSString  *phone_number;
    BOOL isHaving;
}
@property (weak, nonatomic) IBOutlet UITableView *table;
@end

@implementation ContactPersonVC
- (void)viewDidLoad {
    [super viewDidLoad];
   [super setTitleView:self.view withName:@"个人信息设置"];
    phone_number =[BindTool share].bindInfo.phone_number;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setPersonData:) name:@"setting_data" object:nil];
    NSMutableArray *personArray=[[DbManage shareDbManage] queryPersonInfoByUserId:phone_number];
    if (personArray.count!=0) {
        NSLog(@"查出的结果是");
        personInfo =[personArray objectAtIndex:0];
        isHaving =YES;
        url =personInfo.head_pic;
        nick=personInfo.nickname;
        cityName=personInfo.address;
        sign =personInfo.sign;
        if ([personInfo.sex isEqualToString:@"0"]) {
            sex =@"男";
        }else if([personInfo.sex isEqualToString:@"1"]){
            sex =@"女";
        }
    }else{
        personInfo =[[ContactPersonInfo alloc]init];
        isHaving=false;
    }
    _table.delegate=self;
    _table.dataSource=self;
}
-(void)setPersonData:(NSNotification*)sender{
    NSLog(@"setPersonData");
    NSDictionary *data = sender.userInfo;
    NSString *type =[data objectForKey:@"type"];
    NSString *value =[data objectForKey:@"data"];
    if ([type isEqualToString:@"url"]) {
        url =value;
        personInfo.head_pic=value;
    }else if([type isEqualToString:@"nick"]){
        nick =value;
        personInfo.nickname=value;
    }else if([type isEqualToString:@"sign"]){
        sign =value;
        personInfo.sign=value;
    }else if([type isEqualToString:@"sex"]){
        sex=value;
        if ([value isEqualToString:@"男"]) {
            personInfo.sex=@"0";
        }else if([value isEqualToString:@"女"]){
            personInfo.sex=@"1";
        }
    }else if([type isEqualToString:@"city"]){
        cityName=value;
        personInfo.address=value;
    }else if([type isEqualToString:@""]){
        
    }
    [self.table reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ContactPersonCell *cell =[ContactPersonCell setContactPersonCellWithTableView:tableView];
    if (indexPath.row==0) {
        cell.name.text=@"头像";
        cell.phone.hidden=YES;
        cell.nameValue.hidden=YES;
        cell.pic.hidden=NO;
        if (url!=nil) {
            [cell.pic sd_setImageWithURL:[NSURL URLWithString:url]];
        }
    }else if(indexPath.row==1){
        cell.name.text=@"绑定手机号";
        cell.phone.hidden=NO;
        cell.nameValue.hidden=YES;
        cell.pic.hidden=YES;
        cell.arrow.hidden=YES;
        cell.phone.text=phone_number;
    }else if(indexPath.row==2){
        cell.name.text=@"昵称";
        cell.phone.hidden=YES;
        cell.nameValue.hidden=NO;
        cell.pic.hidden=YES;
        cell.arrow.hidden=NO;
        cell.nameValue.text=nick;
    }else if(indexPath.row==3){
        cell.name.text=@"性别";
        cell.phone.hidden=YES;
        cell.nameValue.hidden=NO;
        cell.pic.hidden=YES;
        cell.arrow.hidden=NO;
        cell.nameValue.text=sex;
    }else if(indexPath.row==4){
        cell.name.text=@"所在地";
        cell.phone.hidden=YES;
        cell.nameValue.hidden=NO;
        cell.pic.hidden=YES;
        cell.arrow.hidden=NO;
        cell.nameValue.text=cityName;
    }else if(indexPath.row==5){
        cell.name.text=@"个性签名";
        cell.phone.hidden=YES;
        cell.nameValue.hidden=NO;
        cell.pic.hidden=YES;
        cell.arrow.hidden=NO;
        cell.nameValue.text=sign;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        PersonHeadPicture *pic =[[PersonHeadPicture alloc]init];
        pic.imageUrl=personInfo.head_pic;
        [self.navigationController pushViewController:pic animated:YES];
    }else if(indexPath.row==1){
        
    }else if(indexPath.row==2){
        ContactPersonNick *nick =[[ContactPersonNick alloc]init];
        nick.nickValue =personInfo.nickname;
        [self.navigationController pushViewController:nick animated:YES];
    }else if(indexPath.row==3){
        PersonSex *sex =[[PersonSex alloc]init];
        sex.sexText=personInfo.sex;
        [self.navigationController pushViewController:sex animated:YES];
    }else if(indexPath.row==4){
        //城市选择器啊
        PersonCityChoose *cityChoose =[[PersonCityChoose alloc]init];
        cityChoose.cityValue =personInfo.address;
        [self.navigationController pushViewController:cityChoose animated:YES];
    }else if(indexPath.row==5){
        PersonSign *sign =[[PersonSign alloc]init];
        sign.signValue =personInfo.sign;
        [self.navigationController pushViewController:sign animated:YES];
    }

}
-(void)updatePersonToServer{//上传个人信息
    
    NSString *urlServer =[apiUrl stringByAppendingString:updateUser];
    urlServer =[urlServer stringByAppendingString:phone_number];
    personInfo.user_id=phone_number;
    //NSDictionary *pa =[personInfo mj_keyValues];
    NSDictionary *pa =@{@"address":personInfo.address,@"nickname":personInfo.nickname,
                        @"head_pic":personInfo.head_pic,@"user_id":personInfo.user_id,
                        @"sign":personInfo.sign,@"sex":personInfo.sex};
    NSString *kkk =[pa mj_JSONString];
    NSLog(@"kkkkkkk======%@",kkk);
    [HttpTool Post:urlServer parameters:pa success:^(id responseObject) {
        //NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"str====上传个人信息%@",str);
         RunSucessInfo *success =[RunSucessInfo mj_objectWithKeyValues:responseObject];
        if (success.status) {
            //插入数据库中
            NSMutableArray *array =[NSMutableArray array];
            [array addObject:personInfo];
            if (isHaving) {
                [[DbManage  shareDbManage] updatePersonInfo:array];
            }else{
                [[DbManage  shareDbManage] insertPersonInfoArray:array];
            }
            NSNotification * notice = [NSNotification notificationWithName:@"update_url"object:nil userInfo:@{@"data":personInfo.head_pic}];
            [[NSNotificationCenter defaultCenter]postNotification:notice];//发出了修改成功的通知
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)keep{
    [self updatePersonToServer];
}
@end
